<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://think.ctolog.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\wechat\controller;

use app\wechat\service\TagsService;
use controller\BasicAdmin;
use service\DataService;
use service\LogService;
use service\ToolsService;
use service\WechatService;
use think\Db;

/**
 * 模板消息管理
 * Class Tags
 * @package app\wechat\controller
 */
class Message extends BasicAdmin
{

    /**
     * 定义当前默认数据表
     * @var string
     */
    public $table = 'WechatMessage';

    /**
     * 显示模板消息列表
     * @return array|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\Exception
     */
    public function index()
    {
        $this->title = '模板消息列表管理';
        list($get, $db) = [$this->request->get(), Db::name($this->table)];
        foreach (['name'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike($key, "%{$get[$key]}%");
        }

        return parent::_list($db->order('id desc'));
    }

    /**
     * 新增消息
     * @return array|string
     * @throws \WeChat\Exceptions\InvalidResponseException
     * @throws \WeChat\Exceptions\LocalCacheException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function add()
    {
        if ($this->request->isGet()) {
            return parent::_form($this->table, 'form', 'id');
        }


        //获取授权通行证
        $arr = [
            "touser" => '',
            "template_id" => '',
            "url" => '',
            "miniprogram" => [
                'appid' => [],
                'pagepath' => [],
            ],
            'data' => [
                'first'=>['value'=>'NIHAO'],
                'keyword1'=>['value'=>'巧克力','color'=>'#173177'],
                'keyword2'=>['value'=>'39.8元'],
                'keyword3'=>['value'=>'2014年9月22日'],
            ]
        ];
        //推送消息
        $res =  WechatService::WeChatTemplate()->send($arr);
        return $res; // errcode==0 成功

        if (DataService::save($this->table, $post, 'id')) {
            $this->success('添加消息成功!', '');
        }
        $this->error('添加失败, 请稍候再试!');
    }

    /**
     * 编辑粉丝标签
     * @return array|string
     *
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        // 显示编辑界面
        if ($this->request->isGet()) {
            return parent::_form($this->table, 'form', 'id');
        }
        // 接收提交的数据
        $id = $this->request->post('id', '0');
        $name = $this->request->post('name', '');
        $info = Db::name($this->table)->where(['id' => $id])->find();
        if (empty($info)) {
            $this->error('不存在!');
        }
        $post = $this->request->post();
        $post['constellation'] = ToolsService::get_constellation($post['brithday']);

        //文件上传
        try {
            DataService::save($this->table, $post , 'id');
        } catch (\Exception $e) {
            $this->error('编辑失败, 请稍后再试!' . $e->getMessage());
        }
        $this->success('编辑成功!', '');
    }


    /**
     * 删除
     * @throws \WeChat\Exceptions\InvalidResponseException
     * @throws \WeChat\Exceptions\LocalCacheException
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del()
    {
        $ids = explode(',', $this->request->post('id', ''));
        try{
            foreach ($ids as $id){
                Db::name($this->table)->where(['id' => $id])->delete();
            }

        } catch (\Exception $e){
            $this->error('移除失败，请稍候再试！');
        }
        $this->success('移除成功！', '');
    }


}
